# -*- coding: utf-8 -*-
from Queue import Queue
import termios
import threading
#import Server
__author__ = 'hftec'
queue = Queue(100)

class SerialComunicate():
    tty_file_w = open(u"/dev/ttyS3","w")
    tty_file_r = open(u"/dev/ttyS3","r+")
    new = termios.tcgetattr(tty_file_r)
    new[3] = new[3]&~termios.ECHO
    termios.tcsetattr(tty_file_r,termios.TCSADRAIN,new)
    def __init__(self):
        self.serial_data = threading.Thread(target = self.run)
        self.serial_data.start()
        #threading.Thread.__init__(self,name = "serial")

    def write(self,data):
        self.tty_file_w.write(data)
        self.tty_file_w.flush()
    def run(self):
        while(True):
            data = self.tty_file_r.readline()
	    str_data = ""
	    for v in data:
            	if (ord(v) ==  10) or (ord(v) ==  13):
			str_data += v
		else:
			str_data += ("%x"%ord(v)).zfill(2)
            #str_data = "".join(["%2x"%ord(v) for v in data])
	    print "data from serial:",str_data
            queue.put(str_data)
	    del str_data

serial = SerialComunicate()
